#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "./pyqt5.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then
    echo "请输入正确的构建路径"
    exit 1
fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then
    echo "请输入正确的安装路径"
    exit 1
fi

buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
wget -t 40 -c -P $buildpath https://cfhcable.dl.sourceforge.net/project/pyqt/PyQt5/PyQt-5.6/PyQt5_gpl-5.6.tar.gz
tar -xzvf $buildpath/PyQt5_gpl-5.6.tar.gz -C $buildpath && rm -rf $buildpath/PyQt5_gpl-5.6.tar.gz
cd $buildpath/PyQt5_gpl-5.6
sed -i "2681s/OPEN_SOURCE_LICENSEES/'Open Source'/g" configure.py
echo "yes" | python3 configure.py --qmake=/usr/bin/qmake-qt5 QMAKE_CC=clang QMAKE_CXX=clang++
sed -i "1085s/^/\/\/&/g" ./QtCore/sipQtCoreQt.cpp
sed -i "1098s/^/\/\/&/g" ./QtCore/sipQtCoreQt.cpp
sed -i "276s/^/\/\/&/g" ./QtTest/sipQtTestQTest.cpp

make all -j$(nproc)
make -j$(nproc) install
rm -rf $buildpath/PyQt5_gpl-5.6
